package com.jason.kotlin.activityresult

import android.content.Intent
import android.os.Bundle
import android.provider.SyncStateContract
import android.view.Gravity
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.jason.kotlin.R
import com.jason.kotlin.constant.Constants

/**
 * Description:FirstActivity
 *
 * @author chenbaoyang
 * @create 2021/1/14 13:52
 */
class FirstActivity : AppCompatActivity() {

    private lateinit var container : LinearLayout
    private lateinit var button: Button
    private lateinit var button2: Button
    private lateinit var textView : TextView

    private lateinit var myActivityResultLauncher : ActivityResultLauncher<String>
    private lateinit var launcher : ActivityResultLauncher<Bundle>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //创建根部就
        container = LinearLayout(this)
        val layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT).apply {
            gravity = Gravity.CENTER
        }
        container.apply {
            this.layoutParams = layoutParams
            this.orientation = LinearLayout.VERTICAL
        }

        //创建点击按钮
        button = Button(this)
        button.apply {
            text = "跳转到SecondActivity"
            setOnClickListener {
                onClick(R.id.start_activity_for_result)
            }
        }

        //创建点击按钮
        button2 = Button(this)
        button2.apply {
            text = "跳转MyFragmentActivity"
            setOnClickListener {
                onClick(R.id.start_activity_for_result2)
            }
        }

        //创建展示文案
        textView = TextView(this)

        container.addView(button)
        container.addView(button2)
        container.addView(textView)

        setContentView(container)

        myActivityResultLauncher = registerForActivityResult(MyActivityResultContract()){result ->
            Toast.makeText(applicationContext,result,Toast.LENGTH_SHORT).show()
            textView.text = "回传数据：$result"
        }

        launcher = registerForActivityResult(MyActivityResultContract2(MyFragmentActivity::class.java)) {
                result ->

            result?.let {
                val message = result.getStringExtra("result")
                Toast.makeText(applicationContext, message, Toast.LENGTH_SHORT).show()
                textView.text = "回传数据2：$message"
            }
        }

        //registerForActivityResult(ActivityResultContracts.RequestPermission())
    }


    private fun onClick(viewId : Int) {
        when(viewId) {
            R.id.start_activity_for_result ->  myActivityResultLauncher.launch("Hello,技术最TOP")
            R.id.start_activity_for_result2 -> {
                val bundle = Bundle().apply {
                    putString(Constants.KEY_DATA, "Hello，数据来源FirstActivity")
                }
                launcher.launch(bundle)
            }
        }

    }
}